package com.skypan.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Window;
import android.widget.Button;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.skypan.myapplication.bean.LoginR;
import com.skypan.myapplication.http.LoginCallback;
import com.skypan.myapplication.widget.ClearEditText;
import com.zhy.http.okhttp.OkHttpUtils;

import okhttp3.Call;


public class LoginActivity extends AppCompatActivity implements View.OnClickListener {


    private ClearEditText txtaccount;
    private ClearEditText txtpassword;
    private Button btnlogin;
    private View txttoReg;
    private String url = "http://39.105.165.31:8080/user/login";





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(com.skypan.myapplication.R.layout.activity_login);
        initView();
        }

    private void initView(){
        btnlogin = findViewById(com.skypan.myapplication.R.id.btn_login);
        txttoReg = findViewById(com.skypan.myapplication.R.id.txt_toReg);
        txtaccount = findViewById(com.skypan.myapplication.R.id.etxt_phone);
        txtpassword = findViewById(com.skypan.myapplication.R.id.etxt_pwd);

        btnlogin.setOnClickListener(this);
        txttoReg.setOnClickListener(this);
    }

    public void onClick(View view){
        switch (view.getId()) {
            case com.skypan.myapplication.R.id.btn_login:

                String account = txtaccount.getText().toString().trim();
                if(TextUtils.isEmpty(account)){
                    Toast.makeText(this,  "请输入账号", Toast.LENGTH_SHORT).show();
                    return;
                }

                String password = txtpassword.getText().toString().trim();
                if(TextUtils.isEmpty(password)){
                    Toast.makeText(this,  "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                postLogin(account,password);
                break;
            case com.skypan.myapplication.R.id.txt_toReg:
                Intent intent2 = new Intent(this, RegisterActivity.class);
                startActivity(intent2);
                finish();
                break;
        }

    }


    public void postLogin(String account,String password)
    {
        OkHttpUtils
                .post()//
                .url(url)//
                .addParams("userAccount", "18134744882")//
                .addParams("userPassword","123456")//
                .build()//
                .execute(new LoginCallback()
                {
                    @Override
                        public void onError(Call call, Exception e, int id)
                        {

                        Toast.makeText(LoginActivity.this,"密码错误",Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(LoginR response, int id)
                    {

                        LocalDataApplication.getInstance().setLocalResponse(response);
                        setResult(RESULT_OK);
                        finish();

                        if(response.getCode().equals(0)){
//                            Toast.makeText(LoginActivity.this,response.getMsg(), Toast.LENGTH_SHORT).show();
                        switch (response.data.userIdentity){
                            case "2":
                                Intent intent2 = new Intent(LoginActivity.this, ManagerMainActivity.class);
                                startActivity(intent2);
                                break;
                            case "3":
                                Intent intent3 = new Intent(LoginActivity.this, OperatorMainActivity.class);
                                startActivity(intent3);
                                break;
                            case "4":
                                Intent intent4 = new Intent(LoginActivity.this, TransferorMainActivity.class);
                                startActivity(intent4);
                                break;
                            case "5":
                                Intent intent5 = new Intent(LoginActivity.this, HospitalMainActivity.class);
                                startActivity(intent5);
                                break;
                            case "6":
                                Intent intent6 = new Intent(LoginActivity.this, ClientMainActivity.class);
                                startActivity(intent6);
                                break;
                        }
                        finish();
                        }
                        else Toast.makeText(LoginActivity.this,response.getMsg(),Toast.LENGTH_SHORT).show();

                    }
                });
    }


}
